﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.Models.View
{
    /// <summary>
    /// 交易员月预期
    /// </summary>
    public class OperatorMonthExpected
    {
        public int Id { get; set; }

        /// <summary>
        /// 用户编号
        /// </summary>
        public int OperatorId { get; set; }

        public virtual Sys.Operator Operator { get; set; }

        /// <summary>
        /// 交易年
        /// </summary>
        public int Year { get; set; }
        /// <summary>
        /// 交易月
        /// </summary>
        public int Month { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }

        /// <summary>
        /// 资金利用率
        /// </summary>
        public double Utilization { get; set; }
        /// <summary>
        /// 胜率
        /// </summary>
        public double WinRate { get; set; }
        /// <summary>
        /// 涨幅
        /// </summary>
        public double RaiseRate { get; set; }

        /// <summary>
        /// 每笔盈利
        /// </summary>
        public double AvgBillBenefit { get; set; }

        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }

        /// <summary>
        /// 风险次数
        /// </summary>
        public int RiskCount { get; set; }

        /// <summary>
        /// 收益率
        /// </summary>
        public double Yield { get; set; }
        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }
        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }
        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }

        //public void InitOperatorMonthExpected(int tradeHistoryId, DB.DBManager db)
        //{
        //    List<BenefitHelper.Sys.Operator> operators = db.Operators.ToList();
        //    foreach (BenefitHelper.Sys.Operator op in operators)
        //    {
        //        SetOperatorMonthExpected(op.Id, tradeHistoryId, db);
        //    }
        //    db.SaveChanges();
        //}

        //public void Delete(int year, int month, DB.DBManager db)
        //{
        //    var query = db.OperatorMonthExpected.Where(a => a.Month == month).Where(a => a.Year == year).ToList();
        //    foreach (OperatorMonthExpected op in query)
        //    {
        //        db.OperatorMonthExpected.Remove(op);
        //    }
        //    db.SaveChanges();
        //}

        //public OperatorMonthExpected GetOperatorMonthExpected(int operatorid, int year, int month, DB.DBManager db)
        //{
        //    var query = db.OperatorMonthExpected.Where(a => a.OperatorId == operatorid).Where(a => a.Month == month).Where(a => a.Year == year).OrderByDescending(a => a.Id).ToList();
        //    if (query.Count() > 0)
        //    {
        //        return query.First();
        //    }
        //    else
        //    {
        //        return null;
        //    }

        //}

        ///// <summary>
        ///// 获取操作员周预期(这个方法应该每天去算，现在是从账户周预期里面查到，以后需要优化)
        ///// </summary>
        ///// <param name="operatorid"></param>
        ///// <param name="week"></param>
        ///// <param name="month"></param>
        ///// <param name="year"></param>
        ///// <returns></returns>
        //public void SetOperatorMonthExpected(int operatorid, int tradeHistoryId, DB.DBManager db)
        //{
        //    OperatorMonthExpected change = new OperatorMonthExpected();
        //    BenefitHelper.Sys.TradeHistory th = db.TradeHistorys.Find(tradeHistoryId);
        //    change.Year = th.YPdate;
        //    change.Month = th.MPdate;
        //    change.OperatorId = operatorid;
        //    List<Sys.Account> accounts = new Sys.DayOperatorAccount().GetDayOperatorAccounts(th.GetDateTimeFromTradeHistoryId(tradeHistoryId, db), operatorid, db);
        //    if (accounts.Count > 0)
        //    {
        //        foreach (Sys.Account account in accounts)
        //        {
        //            var query = from t in db.AccountMonthExpected where t.AccountId == account.Id && t.Month == th.MPdate && t.Year == th.YPdate select t;
        //            if (query.Count() > 0)
        //            {
        //                change.BillCount += query.First().BillCount;
        //                change.DayCount += query.First().DayCount;
        //                change.Free += query.First().Free;
        //                change.LoseCount += query.First().LoseCount;
        //                change.OperatorId = operatorid;
        //                change.Profit += query.First().Profit;
        //                change.SumLost += query.First().SumLost;
        //                change.SumWin += query.First().SumWin;
        //                change.UsedMargin += query.First().UsedMargin;
        //                change.WinCount += query.First().WinCount;
        //            }
        //        }
        //    }
        //    db.OperatorMonthExpected.Add(change);
        //}
    }
}
